'use strict'
var { Plot } = require("../../plot");
var { initPointList } = require("../../points");
var { Point } = require("../../Point.js");
var plot = new Plot(document.querySelector("#myCanvas"));
var list = initPointList(1000);


const vmode = {
  vx: 0,
  vy: 0,
};
//------变量声明
var mlen = 10;//数目
var arr = new Array(mlen);
var distance = new Array(mlen);//相关粒子

let grev = 2.8, repu = 0.2; var enum1 = 5; //配置1

window.grev = 0.3, window.repu = 1.5; window.enum1 = 50;
var ifameCount
function step() {
  var lastXY = list.map(v => {
    return { x: v.x, y: v.y }
  })
  for (var i = 0; i < list.length; i++) {

    let p = list[i];
    //当前和其他的
    for (let j = 0; j < list.length; j++) {
      let pv = { dvx: p.x - lastXY[j].x, dvy: p.y - lastXY[j].y };
      distance[j] = Point.len(lastXY[i], lastXY[j])
      if (distance[j] == 0) {
        continue
      }
      var val = -1
      if (p.e ^ list[j].e) {
        val = 1
      }
      list[j].vx += enum1 * pv.dvx / (distance[j] * distance[j] ) * val;
      list[j].vy += enum1 * pv.dvy / (distance[j] * distance[j] ) * val;
      list[i].vx -= enum1 * pv.dvx / (distance[j] * distance[j] ) * val;
      list[i].vy -= enum1 * pv.dvy / (distance[j] * distance[j]) * val;
      
      list[j].vx += enum1 * pv.dvx * (Math.sin(distance[j]) + 0.3) 
      list[j].vy += enum1 * pv.dvy * (Math.sin(distance[j]) + 0.3) 
      list[i].vx -= enum1 * pv.dvx * (Math.sin(distance[j]) + 0.3) 
      list[i].vy -= enum1 * pv.dvy * (Math.sin(distance[j]) + 0.3) 
      var c = cout1(pv.dvx, pv.dvy, distance[j]);
      list[j].vx -= c.vx * pv.dvx;
      list[j].vy -= c.vy * pv.dvy;
      list[i].vx += c.vx * pv.dvx;
      list[i].vy += c.vy * pv.dvy;

    }
    // console.log(i,list[i].x, list[i].y)
    list[i].x += list[i].vx * 0.000001
    list[i].y += list[i].vy * 0.000001
  }

  plot.drawList(list)
  requestAnimationFrame(step);// 
}


console.log("step2");
var laterDraw = Date.now()
function step2() {
  var lastXY = list.map(v => {
    v.vx = 0;
    v.vy = 0;
    return { x: v.x, y: v.y }
  })
  for (var i = 0; i < list.length; i++) {

    let p = list[i];
    //当前和其他的
    for (let j = 0; j < list.length; j++) {
      let pv = { dvx: p.x - lastXY[j].x, dvy: p.y - lastXY[j].y };
      distance[j] = Point.len(lastXY[i], lastXY[j])
      if (distance[j] == 0 || isNaN(distance[j])) {
        continue
      }
      var val = -1
      if (p.e ^ list[j].e) {
        val = 1
      }
      list[j].vx += enum1 * pv.dvx / (distance[j] * distance[j]) * val;
      list[j].vy += enum1 * pv.dvy / (distance[j] * distance[j]) * val;
      list[i].vx -= enum1 * pv.dvx / (distance[j] * distance[j]) * val;
      list[i].vy -= enum1 * pv.dvy / (distance[j] * distance[j]) * val;

      list[j].vx += enum1 * pv.dvx * (Math.sin(distance[j] * distance[j] + distance[j]) + 0.3)
      list[j].vy += enum1 * pv.dvy * (Math.sin(distance[j] * distance[j] + distance[j]) + 0.3)
      list[i].vx -= enum1 * pv.dvx * (Math.sin(distance[j] * distance[j] + distance[j]) + 0.3)
      list[i].vy -= enum1 * pv.dvy * (Math.sin(distance[j] * distance[j] + distance[j]) + 0.3)
      // var c = cout1(pv.dvx, pv.dvy, distance[j]);
      // list[j].vx -= c.vx * pv.dvx;
      // list[j].vy -= c.vy * pv.dvy;
      // list[i].vx += c.vx * pv.dvx;
      // list[i].vy += c.vy * pv.dvy;

    }
    // console.log(i,list[i].x, list[i].y)
    list[i].x += list[i].vx * 0.000001
    list[i].y += list[i].vy * 0.000001
  }

  plot.drawList(list)
  // laterDraw = 
  var fps = 1000 / (Date.now() - laterDraw)
  document.getElementById("fps").innerText = fps;
  laterDraw = Date.now();
  requestAnimationFrame(step2);// 
}

function step3() {
  var lastXY = list.map(v => {
    v.vx = 0;
    v.vy = 0;
    return { x: v.x, y: v.y }
  })
  for (var i = 0; i < list.length; i++) {

    let p = list[i];
    //当前和其他的
    for (let j = 0; j < list.length; j++) {
      let pv = { dvx: p.x - lastXY[j].x, dvy: p.y - lastXY[j].y };
      distance[j] = Point.len(lastXY[i], lastXY[j])
      if (distance[j] == 0 || isNaN(distance[j])) {
        continue
      }
      var val = -1.1
      if (p.e ^ list[j].e) {
        val = 1
      }
      list[j].vx += enum1 * pv.dvx / (distance[j] * distance[j]) * val;
      list[j].vy += enum1 * pv.dvy / (distance[j] * distance[j]) * val;
      list[i].vx -= enum1 * pv.dvx / (distance[j] * distance[j]) * val;
      list[i].vy -= enum1 * pv.dvy / (distance[j] * distance[j]) * val;

      list[j].vx += enum1 * pv.dvx * (Math.sin(distance[j] * distance[j] + distance[j]) + 0.3)
      list[j].vy += enum1 * pv.dvy * (Math.sin(distance[j] * distance[j] + distance[j]) + 0.3)
      list[i].vx -= enum1 * pv.dvx * (Math.sin(distance[j] * distance[j] + distance[j]) + 0.3)
      list[i].vy -= enum1 * pv.dvy * (Math.sin(distance[j] * distance[j] + distance[j]) + 0.3)
      // var c = cout1(pv.dvx, pv.dvy, distance[j]);
      // list[j].vx -= c.vx * pv.dvx;
      // list[j].vy -= c.vy * pv.dvy;
      // list[i].vx += c.vx * pv.dvx;
      // list[i].vy += c.vy * pv.dvy;

    }
    // console.log(i,list[i].x, list[i].y)
    list[i].x += list[i].vx * 0.000001
    list[i].y += list[i].vy * 0.000001
  }

  plot.drawList(list)
  // laterDraw = 
  var fps = 1000 / (Date.now() - laterDraw)
  document.getElementById("fps").innerText = fps;
  laterDraw = Date.now();
  requestAnimationFrame(step3);// 
}

/**
 * 
 * @param {Point3D[]} list 
 */
function step4() {
  var distance = [];
  for (let i = 0; i < list.length; i++) {
    list[i].ax = 0;
    list[i].ay = 0;
    for (let j = 0; j < list.length; j++) {
      // const element = array[index];
      if (i == j) {
        continue
      }
      let p = list[i];

      let pv = { dvx: p.x - list[j].x, dvy: p.y - list[j].y};
      distance[j] = Point.len(list[i], list[j])
      if (distance[j] < 3) {
        val = 1
      } else if (distance[j] < 5) {
        val = -1
      }
      else if (distance[j] < 10) {
        val = 1
      } else {
        var val = -1
      }

      list[i].ax += pv.dvx / (distance[j] * distance[j]) * val
      list[i].ay += pv.dvy / (distance[j] * distance[j]) * val;
      // console.log("v::",[j],list[j].vx, list[j].vy, list[j].vz);
    }
    list[i].vx += list[i].ax;
    list[i].vy += list[i].ay;
    // console.log("xyz::",list[i].x, list[i].y, list[i].z);
    list[i].x += list[i].vx
    list[i].y += list[i].vy
  }
  plot.drawList(list);
  requestAnimationFrame(step4);// 
}

function cout1(dvx, dvy, dist1) { // 计算并返回 相对位移 dvx,dvy,距离dist1
  window.grev = 0.3, window.repu = 1.5;
  var res = Object.create(vmode);
  if ((dist1 > 40)) {
    res.vx = dvx / dist1 * grev;//* Math.sin(t) * Math.sin(t)
    res.vy = dvy / dist1 * grev;
  } else {//if (distance[j] < 100) 
    res.vx = -dvx / dist1 * repu;
    res.vy = -dvy / dist1 * repu;
  }
  return res;
}
function count7(dvx, dvy, dist1) { // 计算并返回 相对位移 dvx,dvy,距离dist1
  window.grev = 0.3, window.repu = 1.5;
  var res = Object.create(vmode);
  var f = 1 / (dist1 * dist1) * 1000000;
  if ((dist1 < 40)) {
    res.vx = dvx / dist1 * f;//* Math.sin(t) * Math.sin(t)
    res.vy = dvy / dist1 * f;
  } else {//if (distance[j] < 100) 
    res.vx = -dvx / dist1 * f;//4//
    res.vy = -dvy / dist1 * f;//4//
  }
  return res;
}
function cout2(dvx, dvy, dist1) { // 计算并返回
  window.grev = 0.3;
  var res = Object.create(vmode);
  // var f = 1 / dist1 - 20;
  var f = dist1 * dist1 - 60 * dist1 + 50;
  // var e2 = Math.sqrt(dvx * dvx + dvy * dvy);
  res.vx = dvx / dist1;
  res.vy = dvy / dist1;
  return res;
}

function cout3(dvx, dvy, dist1) { // 计算并返回
  window.grev = 0.3;
  var res = Object.create(vmode);
  // var f = 1 / dist1 - 20;
  // var e2 = Math.sqrt(dvx * dvx + dvy * dvy);
  res.vx = dvx / dist1;

  res.vy = dvy / dist1;

  return res;
}
function cout4(dvx, dvy, dist1) { // 计算并返回
  window.grev = 0.3;
  var res = Object.create(vmode);
  // var f = 1 / dist1 - 20;
  var f = Math.sin(1 / dist1 * dist1) * 20;//*Math.sin(dist1/5)*20;

  res.vx = dvx / dist1 * f;

  res.vy = dvy / dist1 * f;

  return res;
}
function cout5(dvx, dvy, dist1) { // 计算并返回
  window.grev = 0.3;
  var res = Object.create(vmode);
  // var f = 1 / dist1 - 20;
  var f = Math.sin(1 / (dist1 * dist1)) * 20;//*Math.sin(dist1/5)*20;

  res.vx = dvx / dist1 * f;

  res.vy = dvy / dist1 * f;

  return res;
}
function cout6(dvx, dvy, dist1) { // 计算并返回
  var res = Object.assign({},vmode);
  var f = 1 / (dist1 * dist1) * 1000000;// prev=10000
  res.vx = dvx / dist1 * f;
  res.vy = dvy / dist1 * f;
  return res;
}


window.count = count7; // 大于0 引力

// step3();
step4();